/* eslint-env node */

/*
 * This file runs in a Node context (it's NOT transpiled by Babel), so use only
 * the ES6 features that are supported by your Node version. https://node.green/
 */

// Configuration for your app
// https://v2.quasar.dev/quasar-cli-vite/quasar-config-js

import { defineConfig } from '#q-app/wrappers'
import viteCompression from 'vite-plugin-compression'

const packageJSON = require('./package.json')

export default defineConfig(function (/* ctx */) {
  const alias = process.env.alias ? packageJSON.name : 'dist'
  return {
    eslint: {
      // fix: true,
      // include = [],
      // exclude: [],
      // rawOptions = {},
      warnings: true, errors: true
    },
    // https://v2.quasar.dev/quasar-cli/prefetch-feature
    // preFetch: true,
    // app boot file (/src/boot)
    // --> boot files are part of "main.js"
    // https://v2.quasar.dev/quasar-cli/boot-files
    boot: ['i18n', 'main', 'router'],
    // https://v2.quasar.dev/quasar-cli-vite/quasar-config-js#css
    css: ['app.scss'],
    // https://github.com/quasarframework/quasar/tree/dev/extras
    extras: ['mdi-v7', 'material-icons'],
    // Full list of options: https://v2.quasar.dev/quasar-cli-vite/quasar-config-js#build
    build: {
      target: {
        browser: ['es2019', 'edge88', 'firefox78', 'chrome87', 'safari13.1'],
        node: 'node16'
      },
      vueRouterMode: 'hash', // available values: 'hash', 'history'
      // vueRouterBase,
      // vueDevtools,
      // vueOptionsAPI: false,
      // rebuildCache: true, // rebuilds Vite/linter/etc cache on startup
      // publicPath: '/',
      // analyze: true,
      // env: {},
      // rawDefine: {}
      // ignorePublicFolder: true,
      // minify: false,
      // sourcemap: true,
      // polyfillModulePreload: true,
      distDir: alias,
      extendViteConf (viteConf) {
        viteConf.define.__VUE_PROD_HYDRATION_MISMATCH_DETAILS__ = 'true'
        const list = ['@quasar', '@intlify', '@vue', 'echarts', 'main', 'axios', 'vue-router', 'xlsx', 'konva',
          'mqtt', '@vue-flow']
        const timeStamp = Date.now()
        viteConf.base = '' // 配置打包后文件地址为相对路径
        viteConf.build.rollupOptions = {
          output: {
            manualChunks (id) { // 静态资源分拆打包
              if (id.includes('node_modules')) {
                const name = id.toString().split('node_modules/')[1].split('/')[0]
                if (['zrender', 'tslib'].includes(name)) return 'zrender'
                if (name === 'quasar') {
                  const alias = id.toString().split('node_modules/quasar/')[1].split('/')[0]
                  return alias === 'lang' ? 'quasarLang' : 'quasar'
                }
                if (list.includes(name)) return name
                return 'packages'
              }
              // if (id.includes('src/views/')) return id.toString().split('src/views/')[1].split('/')[0]
              if (id.includes('src/views/')) return 'views'
              if (id.includes('src/components')) return 'components'
            },
            chunkFileNames: `assets/[name]-${timeStamp}.js`,
            entryFileNames: `assets/[name]-${timeStamp}.js`,
            assetFileNames: e => (e.name || '').endsWith('.css') ? `assets/[name]-${timeStamp}.[ext]`
              : 'assets/[name].[ext]'
          }
        }
      },
      // viteVuePluginOptions: {},
      vitePlugins: [
        [viteCompression, {
          ext: '.gz',
          deleteOriginFile: false,
          verbose: true, // 默认即可
          disable: false, // 开启压缩(不禁用)，默认即可
          threshold: 10240, // 压缩前最小文件大小
          algorithm: 'gzip' // 压缩算法
        }, { server: false }],
        ['vite-plugin-checker', {
          eslint: { lintCommand: 'eslint -c ./eslint.config.js "./src*/**/*.{js,mjs,cjs,vue}"', useFlatConfig: true }
        }, { server: false }]
      ]
    },
    // Full list of options: https://v2.quasar.dev/quasar-cli-vite/quasar-config-js#devServer
    devServer: {
      // https: true,
      open: false // opens browser window automatically
    },
    // https://v2.quasar.dev/quasar-cli-vite/quasar-config-js#framework
    framework: {
      config: {
        loadingBar: { color: 'primary', size: '1px', skipHijack: true },
        notify: {
          position: 'top', timeout: 5000, progress: true, progressClass: 'bg-grey-6',
          actions: [{ icon: 'close', color: 'grey-5', dense: true }]
        }
      },

      iconSet: 'material-icons', // Quasar icon set
      lang: 'zh-CN', // Quasar language pack
      // For special cases outside where the auto-import strategy can have an impact
      // (like functional components as one of the examples),
      // you can manually specify Quasar components/directives to be available everywhere:
      //
      // components: [],
      // directives: [],
      // Quasar plugins
      plugins: ['AppFullscreen', 'Cookies', 'Dialog', 'LoadingBar', 'LocalStorage', 'Notify', 'SessionStorage']
    },
    animations: 'all', // --- includes all animations
    // https://v2.quasar.dev/options/animations
    // animations: [],

    // https://v2.quasar.dev/quasar-cli-vite/quasar-config-js#property-sourcefiles
    // sourceFiles: {
    //   rootComponent: 'src/App.vue',
    //   router: 'src/router/index',
    //   store: 'src/store/index',
    //   registerServiceWorker: 'src-pwa/register-service-worker',
    //   serviceWorker: 'src-pwa/custom-service-worker',
    //   pwaManifestFile: 'src-pwa/manifest.json',
    //   electronMain: 'src-electron/electron-main',
    //   electronPreload: 'src-electron/electron-preload'
    // },

    // Full list of options: https://v2.quasar.dev/quasar-cli/developing-electron-apps/configuring-electron
    electron: {
      // extendElectronMainConf (esbuildConf)
      // extendElectronPreloadConf (esbuildConf)
      inspectPort: 5858,
      bundler: 'packager', // 'packager' or 'builder'
      packager: {
        // https://github.com/electron-userland/electron-packager/blob/master/docs/api.md#options
        // OS X / Mac App Store
        // appBundleId: '',
        // appCategoryType: '',
        // osxSign: '',
        // protocol: 'myapp://path',
        // Windows only
        // win32metadata: { ... }
      },
      builder: {
        // https://www.electron.build/configuration/configuration
        appId: 'xiaoy-project'
      }
    }
  }
})